I have an issue with a drop down menu that I am hoping you all can help me with. The issue is when I press "Submit", my code prints back values from a form I developed (eventually I will write them to a database). In this form, I have a dynamically built drop down (php/ mysql query). No matter which value I select, it always prints the LAST row in the table (array) as the selected value. My code follows:
1) I connect to database
2) I am building a set of forms using a loop statement where the variable '$a' increments by one each time through the loop.
3) I run code to get drop down array -
$TVSQL = mysql_query("SELECT TVStation FROM NCF_tvstations");
-
echo "<form action=something.php method=POST>
-
<select name=TVStation[$a]>";
-
while ($r = mysql_fetch_array($TVSQL))
-
{ $TVStation[$a] = $r["TVStation"]; echo "<option
-
value='$TVStation[$a]'>$TVStation[$a]</option>"; }
-
echo "</select></td>";
-
4) The web page for reference is: http://theweekly13.com/test/gamebuilder.php
Thanks for any help.
Cheers!
Seth
Again, your select name on line 2 is an array. This will not be able to be sent. The name needs to be scalar ("BBC" or "TVStation1") instead of an array like (TVStation[1]).
$_POST does not take variables in their arrays like you have ($_POST['$TVStration[$a]']). It takes the anme of the input that was sent, so if your select was called "TVStation1" (note it is not an array), then to call the value of that you would use $_POST['TVStation1'] (not no $ signs which denote variables).
To replace your code use: - $TVSQL = mysql_query("SELECT TVStation FROM NCF_tvstations ORDER BY TVStation ASC");
-
echo "<select name=TVStation".$a."><option value=''>Select TV</option>"; /*** Changed your name to output a scalar rather than an array*/
-
while ($z = mysql_fetch_array($TVSQL))
-
{
-
$TVStation_temp = $z["TVStation"]; /*** Changed your option value variable to a scalar variable rather than an array*/
-
echo "<option value='$TVStation_temp'>$TVStation_temp</option>"; /*** Changed your option value variable to a scalar variable rather than an array (corresponding to previous change)*/
-
}
-
-
echo "</select></td>";
-
echo( $_POST['TVStation'.$a] );/*** Added an echo (if you don't use echo or print, nothing will be displayed), changed name to correspond to new select name in the form of TVStation1*/
-
I have tried to use your code without changing anything more than needs to be changed, but read my comments and make sure you understand why that is the case. Two basic changes are: No array variable names or values, and no variables or arrays within $_POST.
11 3636
Hi Seth,
I am not sure why you are using an array in your loop, but you have not defined what $a is. Your select name is TVStation[$a] which is not a variable like you have in your while loop (which is $TVStation[$a]?? Is that intentional?
In your while loop, you reset what $TVStation[$a] is every loop, and because $a is not incrimented as you said your 'array' $TVStation is not an array, but rather a single value which will be the last TVStation pulled from MySQL.
To fix this you need:
in your while loop.
How are you printing your selected value? Is it: - echo $_POST['TVStation[??]'];
where ?? is what ever $a's original value is? Give us some more information with how you display that as well us you're updated form code (if you choose to update that) and we can see where the mix up is.
I am using
. Whenever I use $POST_, I lose the values and nothing prints.
I am using
to increment the variable each time the loop runs. Is
a better method to accomplish this?
Here is the entire code in case you are wondering - a lot of this is in-progress and requires clean-up: -
<?php
-
-
//Week Variable
-
-
$Season_Start = mktime(0,0,0,8,30,2009);
-
$Season_End = mktime(0,0,0,12,8,2009);
-
$Current_Date = mktime(date("H"), date("i"), date("s"), date("m"), date("d"), date("Y"));
-
-
// Gets session variable for 'Year'
-
// Gets session variable for 'Week'
-
-
$Year = date("Y",$Season_Start);
-
$h = (7*24*60*60);
-
IF ($Current_Date < $Season_Start)
-
{
-
$Week = 1;
-
-
}
-
ELSE
-
{
-
IF ($Current_Date < $Season_End)
-
{
-
$Week = CEIL((($Current_Date-$Season_Start)/$h));
-
}
-
ELSE
-
{
-
$Week = CEIL(($Season_End-$Season_Start))/$h +1;
-
}
-
}
-
-
//Connect to Database
-
-
$dbhost = "XXXXXX";
-
$dbuser = "XXXX";
-
$dbpass = "XXXXXXX";
-
-
function dbConnect($db="") {
-
global $dbhost, $dbuser, $dbpass;
-
-
$dbcnx = @mysql_connect($dbhost, $dbuser, $dbpass)
-
or die("The site database appears to be down.");
-
-
if ($db!="" and !@mysql_select_db($db))
-
die("The site database is unavailable.");
-
-
return $dbcnx;
-
}
-
-
dbconnect("theweekl_test");
-
-
?>
-
-
<!doctype html public "-//w3c//dtd html 3.2//en">
-
<html>
-
<head>
-
<title>The Weekly13 :: Gamebuilder</title>
-
-
<!-- Java script for OnChange of League to bring up Gamebuilder -->
-
-
<SCRIPT language=JavaScript>
-
-
function reload_A(form)
-
{
-
var val=form.League.options[form.League.options.selectedIndex].value;
-
self.location='gamebuilder.php?League=' + val ;
-
}
-
-
function reload_C(form)
-
{
-
var val=form.League.options[form.League.options.selectedIndex].value;
-
var val2=form.GameCount.options[form.GameCount.options.selectedIndex].value;
-
-
self.location='gamebuilder.php?League=' + val + '&GameCount=' + val2 ;
-
}
-
-
</script>
-
-
<style type="text/css">
-
-
p1 {
-
font-family: eurostile, arial, verdana;
-
font-size: 22px;
-
border: 4px solid #cd0000;
-
text-align: center;
-
color: #000066;
-
font-weight: 900;
-
}
-
-
</style>
-
-
</head>
-
-
<body
-
style="background-image: url(http://theweekly13.com/images/core/pabstheaderbg.jpg);">
-
-
<?php //header
-
$curl = curl_init();
-
curl_setopt ($curl, CURLOPT_URL, "http://theweekly13.com/include/header.php");
-
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
-
-
$result = curl_exec ($curl);
-
curl_close ($curl);
-
echo $result;
-
-
echo "<br><br>";
-
-
$WeekIDSQL = mysql_query("SELECT WeekID FROM NCF_weeks WHERE Week = $Week");
-
$WeekID = mysql_result($WeekIDSQL, 0);
-
-
@$League=$_GET['League'];
-
-
/* Data for League DropDown Box */
-
-
$queryUL = mysql_query("SELECT League, LeagueID FROM NCF_leagues
-
ORDER BY LeagueID ASC");
-
-
echo "<form method=POST name=League_GameCount action=' '>";
-
-
/* First Drop down Menu */
-
-
echo "<table
-
style=\"width: 500px; text-align: left; margin-left: auto; margin-right: auto;\"
-
border=\"0\" cellpadding=\"4\" cellspacing=\"4\"><tbody><tr><td style=\"width: 250px; text-align: right;\">
-
<p>Select League:</p></td><td style=\"width: 237px;\">";
-
-
echo "<select name='League' onchange=\"reload_A(this.form)\"> <option value=''><p>Select League</p></option>";
-
-
while($r = mysql_fetch_array($queryUL)) {
-
if($r['League']==@$League){
-
echo "<option selected value='$r[League]'>$r[League]</option>"."<BR>";}
-
else{echo "<option value='$r[League]'>$r[League]</option>";}
-
}
-
echo "</select></td></tr>";
-
-
$_SESSION['$League'] = $League;
-
-
$queryGC = mysql_query("SELECT GameCount, GameCountID FROM NCF_gamecount ORDER BY gamecountID ASC");
-
-
/* Second Drop down Menu */
-
-
echo "<tr><td style=\"text-align: right;\"><p>Select Game Count:</p></td>
-
<td><select name='GameCount' onchange=\"reload_C(this.form)\"><option value=''><p>Change Game Count</p></option>";
-
while($t = mysql_fetch_array($queryGC)) {
-
echo "<option value='$t[GameCount]'>$t[GameCount]</option>";
-
}
-
-
$_SESSION['$GameCount'] = $GameCount;
-
-
echo "</select></td></tr></tbody></table>";
-
echo "<hr style=\"width: 800px; height: 4px; text-align: center;\">";
-
echo "</form>";
-
-
/* ONCE GAMECOUNT IS SELECTED, GAME INFORMATION BOXES APPEAR */
-
-
if(isset($GameCount) )
-
{
-
-
echo "<form method=POST name=Gamebuilder action=''>";
-
-
echo "<table style=\"background-color: #FFFF99; width: 800px; text-align: left;
-
margin-left: auto; margin-right: auto;\"
-
border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tbody><tr>
-
<td style=\"text-align: left; \"><p1>  You are building ".$GameCount.' Games for the
-
'.$League.' league for Week '.$Week." </p1></td></tr></tbody></table><br>";
-
-
$a = 1;
-
WHILE ($a <= $GameCount) {
-
-
IF ($a < 10) {$GameNum = "0".$a; } else {$GameNum = $a;}
-
-
$LeagueNumSQL = mysql_query("SELECT LeagueNumber FROM NCF_leagues WHERE League LIKE '$League'");
-
$LeagueNum = mysql_result($LeagueNumSQL, 0);
-
-
$GameID[$a] = $Year.$WeekID.$GameNum.$LeagueNum;
-
-
echo "<table style=\"width: 800px; text-align: left; margin-left: auto; margin-right: auto;\"
-
border=\"3\" cellpadding=\"1\" cellspacing=\"1\">
-
<tbody><tr><td style=\"background-color: rgb(255, 255, 153);\" colspan=\"6\" rowspan=\"1\"><p>
-
Game ".$a." :: Game ID ".$GameID[$a]."</p></td></tr>";
-
-
echo "<tr><td style=\"width: 289px; text-align: center;\" colspan=\"2\" rowspan=\"1\">Date/
-
Time: <input size=\"25\" name=\"DateTimes[$a]\" value=\"YYYY-MM-DD HH:MM:SS\"></td>";
-
-
echo "<td style=\"width: 100px; text-align: center;\">";
-
-
$TVSQL = mysql_query("SELECT TVStation FROM NCF_tvstations");
-
echo "<form action=something.php method=POST> <select name=$TVStation[$a]>";
-
while ($r = mysql_fetch_array($TVSQL))
-
{ $TVStation[$a] = $r["TVStation"]; echo "<option value='$TVStation[$a]'>$TVStation[$a]</option>"; }
-
echo "</select></td>";
-
-
echo "<td style=\"width: 389px;\" colspan=\"3\" rowspan=\"1\">
-
GameNotes: <input size=\"45\" name=\"GameNotes".$a."\"></td>
-
</tr><tr><td colspan=\"3\" rowspan=\"1\">Away Team Information</td><td colspan=\"3\" rowspan=\"1\">Home Team
-
Information</td></tr><tr><td style=\"text-align: right; width: 75px;\"># <input
-
size=\"5\" name=\"AwayRank".$a."\">.</td>";
-
-
echo "<td style=\"width: 189px; text-align: center;\">";
-
-
$AwayTeamSQL = mysql_query("SELECT College FROM NCF_teams");
-
echo "<form action=something.php method=POST><select name=AwayTeam>";
-
while ($r = mysql_fetch_array($AwayTeamSQL))
-
{ $AwayTeam = $r["College"]; echo "<option value='$AwayTeam'>$AwayTeam</option>"; }
-
echo "</select></td>";
-
-
echo "<td style=\"width: 100px;\">Line: <input size=\"5\" name=\"AwayLine".$a."\" value=\"0.0\"></td>
-
<td style=\"text-align: right; width: 75px;\"># <input size=\"5\" name=\"HomeRank".$a."\">.</td>";
-
-
echo "<td style=\"width: 189px; text-align: center;\">";
-
-
$HomeTeamSQL = mysql_query("SELECT College FROM NCF_teams");
-
echo "<form action=something.php method=POST><select name=HomeTeam>";
-
while ($r = mysql_fetch_array($HomeTeamSQL))
-
{ $HomeTeam = $r["College"]; echo "<option value='$HomeTeam'>$HomeTeam</option>"; }
-
echo "</select></td>";
-
-
echo "</td><td style=\"width: 100px;\">Line: <input size=\"5\" name=\"HomeLine".$a."\" value=\"0.0\"></td>
-
</tr><tr><td style=\"width: 389px;\" colspan=\"3\" rowspan=\"1\">
-
Record: <input size=\"4\" name=\"AwayOAW".$a."\">-<input size=\"4\" name=\"AwayOAL".$a."
-
\">, <input size=\"4\" name=\"AwayCW".$a."\">-<input size=\"4\" name=\"AwayCL".$a."\">
-
</td><td style=\"width: 389px;\" colspan=\"3\" rowspan=\"1\">Record: <input size=\"4\"
-
name=\"HomeOAW".$a."\">-<input size=\"4\" name=\"HomeOAL".$a."\">, <input size=\"4\"
-
name=\"HomeCW".$a."\">-<input size=\"4\" name=\"HomeCL".$a."\"></td></tr></tbody></table><br>";
-
-
$GameInsert = "INSERT INTO NCF_games (GameID, Game, WeekID, League, Year, DateTimes, Awayteam, AwayLine,
-
HomeTeam, HomeLine, GameNotes, GameTV, Notes) VALUES ($GameID, $a, $WeekID, $LeagueNum, $Year, $DateTimes.$a,
-
$AwayTeam, $AwayLine, $HomeTeam, $HomeLine, $GameNotes, $TVStation, $CurrentDate)";
-
-
$GameInfoInsert = "INSERT INTO NCF_gameinfo (GameID, AwayTeam, AwayRank, AwayOAW, AwayOAL, AwayCW, AwayCL,
-
HomeTeam, HomeRank, HomeOAW, HomeOAL, HomeCW, HomeCL, Notes) VALUES ($GameID, $AwayTeam, $AwayRank,
-
$AwayOAW, $AwayOAL, $AwayCW, $AwayCL, $HomeTeam, $HomeRank, $HomeOAW, $HomeOAL, $HomeCW,
-
$HomeCL, $CurrentDate)";
-
-
$a = $a + 1;
-
}
-
-
echo "<div style=\"text-align: center;\"><input value=\"Reset Form\" type=\"reset\"> <input
-
name=\"submitok\" value=\"Submit\" type=\"submit\"></div></form> ";
-
-
$UserCountSQL = mysql_query("SELECT COUNT(UserName) FROM NCF_userstemp
-
WHERE UserLeague = $LeagueNum AND UserName NOT LIKE '%SELECT%'");
-
$UserCount = mysql_result($UserCountSQL, 0);
-
-
$UserSelectSQL = mysql_query("SELECT UserName FROM NCF_userstemp
-
WHERE UserLeague = $LeagueNum AND UserName NOT LIKE '%SELECT%'");
-
-
$UserIDSelectSQL = mysql_query("SELECT UserID FROM NCF_userstemp
-
WHERE UserLeague = $LeagueNum AND UserName NOT LIKE '%SELECT%'");
-
-
-
if (isset($_POST['submitok'])) {
-
-
$b = 1;
-
WHILE ($b <= $GameCount) {
-
-
echo $GameID[$b]." ".$DateTimes[$b]." ".$TVStation[$b]."<br>";
-
$b = $b +1;
-
-
}
-
-
}
-
}
-
else { echo "<br><br><div style=\"text-align: center;\"><p1>Enter Game Count & Select League</p1></div>"; }
-
-
?>
-
-
</body>
-
</html>
-
I won't read all your code, but I think I see your problem.
As explained before in your while loop when you have: - while ($r = mysql_fetch_array($TVSQL))
-
{ $TVStation[$a] = $r["TVStation"]; echo "<option...
You are resetting $TVStation[$a] and overwriting it every time, so when you echo $TVStation[$a] you will end up with the last $r["TVStation"] that you set it at, which coincides with the last item on your select dropdown. It is not your dropdown that's failing.
The problem is you can't $_POST an array without serializing first, which basically turns the array into a string which can be unserialized (turned back into an array) after the $_POST data has been sent and received. You can read about it here or from any Google search about it.
If you do not use $_POST, you are not looking at any data from the form, but only that which you have in the PHP of the current page. If that doesn't make sense, you should read up on how forms are submitted and data is retrieved, and then the difference between $_POST and $_GET which will help you a lot.
I don't think that you need to be serializing, but instead, simply not to have your select name as an array... In other words, don't send arrays in $_POST.
And yes, $a++; is valid and faster, if you simply want an increment of 1.
I am still having trouble getting what's going on with the code I use. I use the exact same coding logic twice before on the same page but outside of the loop statement without issue.
I am using the loop statement because I am building multiple events; quantity determined by the user. The TV Station is a part of the event (the channel that the game is on), which can change for each event.
In your first response, you say:
In your while loop, you reset what $TVStation[$a] is every loop, and because $a is not incremented as you said your 'array' $TVStation is not an array, but rather a single value which will be the last TVStation pulled from MySQL.
. I increment $a at the end of the loop. Also I don't see how I am resetting $TVStation[$a] When I view page source, I get the variables "TVStation[1]", "TVStation[2]", etc. However when I echo $TVStation[$a]; I always get the last row of the table, as if it is preselected for me.
As always, thanks for your help and patience. I am a hobbyist programmer, so most of my code is cobbled together from what I read on the net or get from sites like this one.
You are quoting me from my first post which was a response to your first where you had no mention of your incrementing of $a, but you included that part in your second post which I acknoledged in my second post, and simply pointed out another incrementing technique available to PHP (and some other languages): - $a++; /* instead of */ $a = $a + 1;
On line 189 in the code above you are setting a value to $TVStation[$a]. Everytime you set a value you overwrite the previous value and so the last value which $TVStation[$a] is set at will be the last record from the database. That is why when you echo it you get the last row.
If you want to submit the form and use what was submitted (for example with an echo), you need to use $_POST or $_GET. These do not take arrays as variables and so the bottom line is: You cannot use an array as a form element name*.
* Forget what I mentioned before about serializing, I can't think of a way to use that in a form which is $_POSTed.
Hope that helps.
I apologize for confusing the two quotes.
I changed the code to this -
$TVSQL = mysql_query("SELECT TVStation FROM NCF_tvstations ORDER BY TVStation ASC");
-
echo "<select name=TVStation[$a]><option value=''>Select TV</option>";
-
WHILE ($z = mysql_fetch_array($TVSQL))
-
{
-
$TVStation[$a] = $z["TVStation"];
-
echo "<option value='$TVStation[$a]'>$TVStation[$a]</option>";
-
}
-
-
echo "</select></td>";
-
$_POST['$TVStation[$a]'];
-
It still does not work. I tried about 50 different flavors, including just using a plain old HTML select option list and could not solve the issue. I am at a loss of how to accomplish this, maybe I can find a way without looping - which will be a real pain, but it's doable.
Thanks for your help.
Again, your select name on line 2 is an array. This will not be able to be sent. The name needs to be scalar ("BBC" or "TVStation1") instead of an array like (TVStation[1]).
$_POST does not take variables in their arrays like you have ($_POST['$TVStration[$a]']). It takes the anme of the input that was sent, so if your select was called "TVStation1" (note it is not an array), then to call the value of that you would use $_POST['TVStation1'] (not no $ signs which denote variables).
To replace your code use: - $TVSQL = mysql_query("SELECT TVStation FROM NCF_tvstations ORDER BY TVStation ASC");
-
echo "<select name=TVStation".$a."><option value=''>Select TV</option>"; /*** Changed your name to output a scalar rather than an array*/
-
while ($z = mysql_fetch_array($TVSQL))
-
{
-
$TVStation_temp = $z["TVStation"]; /*** Changed your option value variable to a scalar variable rather than an array*/
-
echo "<option value='$TVStation_temp'>$TVStation_temp</option>"; /*** Changed your option value variable to a scalar variable rather than an array (corresponding to previous change)*/
-
}
-
-
echo "</select></td>";
-
echo( $_POST['TVStation'.$a] );/*** Added an echo (if you don't use echo or print, nothing will be displayed), changed name to correspond to new select name in the form of TVStation1*/
-
I have tried to use your code without changing anything more than needs to be changed, but read my comments and make sure you understand why that is the case. Two basic changes are: No array variable names or values, and no variables or arrays within $_POST.
Okay, so this is starting to work. The $TVStation variable is echoing back within the loop - where you have - echo( $_POST['TVStation'.$a] );
Thanks for your help. This is a big step forward.
No worries, let me know how you go. Once you understand what form of input each function/procedure takes, it will come naturally, and within a month or two, you will look back at this script you're writing and re-do the whole thing more efficiently.
I got this to work in its entirety today. My biggest confusion was that I did not know $Variable[#] was an array! Now that I know that, what you were saying makes perfect sense.
You're right, I will go through the code and do some massive cleaning up. This is actually my second time through. The first time was all hard coded tables without looping. It was arduous to deal with.
Thanks again for your help.
Glad you have it. Hope to see you around Bytes more often, I'm sure we can help speed your understanding and get you teaching others soon ;)
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Jonny Tango |
last post by:
Hello everyone.
Q. How do I create a dynamically-generated drop-down list for use in
an array.
I'm using PHP with a MySQL database (through phpMyAdmin)
My database table is called...
|
by: Greg Scharlemann |
last post by:
I am attempting to populate a drop down menu based on the selection of
a different drop down menu. However, it is not working correctly, I
cannot figure out for the life of me what exactly happens...
|
by: Greg Scharlemann |
last post by:
I would like to automatically populate a drop down menu when the page
loads based on the selection of an item in a different drop down menu.
I made a test page that when drop down #1 changes, drop...
|
by: Yuk Cheng |
last post by:
<<<start index.htm>>>
<html>
<head>
<script>
function perform(action){
}
</script>
</head>
|
by: hemanth.singamsetty |
last post by:
Hello there,
I've a drop down menu (created using CSS & Javascript -- see code
below).
My problem is, whenever I click a link on the menu
the new page replaces the current page (and the menu...
|
by: barbarowa |
last post by:
I've coded a script to populate a drop down menu from a database but I
can't seem to get the PHP script to pass the selected item. The
database only has two fields, ID and ITEM. I want the user...
|
by: phpnewb |
last post by:
Hi, I know i'm doing it wrong, but I'm using a while loop right now to create several instances of a drop down menu. It gives me undesirable results. Can you tell me the right way to do it. Below are...
|
by: rsteph |
last post by:
I have a javascript drop down menu that I borrowed from a website. It utilizes a little .css to help with formatting. The menu works great, and on all 3 of the browsers I'm concerned about; but I am...
|
by: phpnewbie26 |
last post by:
My current form has one multiple select drop down menu as well as few other drop down menus that are single select. Originally I had it so that the multiple select menu was first, but this created...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |